package com.xiangban.volunteer_platform.controller;

import com.xiangban.volunteer_platform.entity.BusinessUser;
import com.xiangban.volunteer_platform.service.BusinessUserService;
import com.xiangban.volunteer_platform.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

/**
 * @Author Mr. Ma
 * @Data 2021/1/20 12:42
 */
@RestController
@RequestMapping("api/user")
@Api(tags = "商家信息控制器")
public class BusinessUserController {

    @Autowired
    private BusinessUserService businessUserService;

    @PostMapping("/saveUser")
    @ApiOperation("增加或修改用户")
    public JsonData addOrUpdate(@RequestBody BusinessUser businessUser){
        int save = businessUserService.addUpdateUser(businessUser);
        if (save == 0){
            return JsonData.buildError(save,"操作失败");
        }
        return JsonData.buildSuccess(businessUser.getId());
    }


    @PostMapping("/deleteUser")
    @ApiOperation("删除用户")
    public JsonData deleteUser(@RequestBody BusinessUser businessUser){
        int i = businessUserService.deleteUser(businessUser);
        if (i == 0){
            return JsonData.buildError("没有此人信息");
        }
        return JsonData.buildSuccess("删除成功");
    }

    @PostMapping("/getUserById")
    @ApiOperation("根据id获取对象")
    public JsonData getUserById(@RequestBody BusinessUser businessUser){
           return JsonData.buildSuccess(businessUserService.findUserById(businessUser));
    }

    @PostMapping("/getAllUser")
    @ApiOperation("获取所有对象信息")
    public JsonData getAllUser(@RequestParam(required = false) Integer pageNum,
                               @RequestParam(required = false) Integer pageSize,@RequestBody BusinessUser businessUser){
           return JsonData.buildSuccess(businessUserService.findAllUser(pageNum,pageSize,businessUser));
    }

    @GetMapping("/getBusinessUser")
    @ApiOperation("获取所有商家")
    public JsonData getBusinessUser(){
       return JsonData.buildSuccess(businessUserService.getABusinessllUser());
    }
}
